package com.zouxw.utils.json.utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * 快捷键管理器
 * 提供统一的快捷键注册和管理功能
 * 
 * @author zouxw
 * @version 1.0
 */
public class KeyboardShortcutManager {
    
    // ==================== 快捷键定义 ====================
    /** 打开文件快捷键 */
    public static final String OPEN_FILE = "OPEN_FILE";
    
    /** 保存文件快捷键 */
    public static final String SAVE_FILE = "SAVE_FILE";
    
    /** 撤销快捷键 */
    public static final String UNDO = "UNDO";
    
    /** 重做快捷键 */
    public static final String REDO = "REDO";
    
    /** 格式化JSON快捷键 */
    public static final String FORMAT_JSON = "FORMAT_JSON";
    
    /** 压缩JSON快捷键 */
    public static final String COMPRESS_JSON = "COMPRESS_JSON";
    
    /** 搜索快捷键 */
    public static final String SEARCH = "SEARCH";
    
    /** 展开所有快捷键 */
    public static final String EXPAND_ALL = "EXPAND_ALL";
    
    /** 折叠所有快捷键 */
    public static final String COLLAPSE_ALL = "COLLAPSE_ALL";
    
    /** 解析JSON快捷键 */
    public static final String PARSE_JSON = "PARSE_JSON";
    
    /** 清空内容快捷键 */
    public static final String CLEAR_CONTENT = "CLEAR_CONTENT";
    
    /** Schema验证快捷键 */
    public static final String SCHEMA_VALIDATION = "SCHEMA_VALIDATION";
    
    // ==================== 实例变量 ====================
    /** 快捷键映射 */
    private final Map<String, Action> shortcutActions;
    
    /** 父组件 */
    private final Container parentComponent;
    
    // ==================== 构造函数 ====================
    
    /**
     * 构造函数
     * 
     * @param parentComponent 父组件
     */
    public KeyboardShortcutManager(Container parentComponent) {
        this.parentComponent = parentComponent;
        this.shortcutActions = new HashMap<>();
    }
    
    // ==================== 公共方法 ====================
    
    /**
     * 注册快捷键
     * 
     * @param shortcutKey 快捷键标识
     * @param keyStroke 按键组合
     * @param action 执行的动作
     */
    public void registerShortcut(String shortcutKey, KeyStroke keyStroke, Action action) {
        shortcutActions.put(shortcutKey, action);
        
        // 注册到输入映射（需要转换为JComponent）
        if (parentComponent instanceof JComponent) {
            JComponent jComponent = (JComponent) parentComponent;
            InputMap inputMap = jComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            ActionMap actionMap = jComponent.getActionMap();
            
            inputMap.put(keyStroke, shortcutKey);
            actionMap.put(shortcutKey, action);
        }
    }
    
    /**
     * 注册默认快捷键
     * 
     * @param actionListener 动作监听器
     */
    public void registerDefaultShortcuts(ActionListener actionListener) {
        // Ctrl+O - 打开文件
        registerShortcut(OPEN_FILE, 
                KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onOpenFile();
                    }
                });
        
        // Ctrl+S - 保存文件
        registerShortcut(SAVE_FILE, 
                KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onSaveFile();
                    }
                });
        
        // Ctrl+Z - 撤销
        registerShortcut(UNDO, 
                KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onUndo();
                    }
                });
        
        // Ctrl+Y - 重做
        registerShortcut(REDO, 
                KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onRedo();
                    }
                });
        
        // Ctrl+Shift+F - 格式化JSON
        registerShortcut(FORMAT_JSON, 
                KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onFormatJson();
                    }
                });
        
        // Ctrl+Shift+C - 压缩JSON
        registerShortcut(COMPRESS_JSON, 
                KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onCompressJson();
                    }
                });
        
        // Ctrl+F - 搜索
        registerShortcut(SEARCH, 
                KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onSearch();
                    }
                });
        
        // Ctrl+E - 展开所有
        registerShortcut(EXPAND_ALL, 
                KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onExpandAll();
                    }
                });
        
        // Ctrl+Shift+E - 折叠所有
        registerShortcut(COLLAPSE_ALL, 
                KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onCollapseAll();
                    }
                });
        
        // Ctrl+Enter - 解析JSON
        registerShortcut(PARSE_JSON, 
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onParseJson();
                    }
                });
        
        // Ctrl+Delete - 清空内容
        registerShortcut(CLEAR_CONTENT, 
                KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onClearContent();
                    }
                });
        
        // Ctrl+Shift+V - Schema验证
        registerShortcut(SCHEMA_VALIDATION, 
                KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK),
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        actionListener.onSchemaValidation();
                    }
                });
    }
    
    /**
     * 获取快捷键描述
     * 
     * @param shortcutKey 快捷键标识
     * @return 快捷键描述
     */
    public String getShortcutDescription(String shortcutKey) {
        switch (shortcutKey) {
            case OPEN_FILE: return "Ctrl+O";
            case SAVE_FILE: return "Ctrl+S";
            case UNDO: return "Ctrl+Z";
            case REDO: return "Ctrl+Y";
            case FORMAT_JSON: return "Ctrl+Shift+F";
            case COMPRESS_JSON: return "Ctrl+Shift+C";
            case SEARCH: return "Ctrl+F";
            case EXPAND_ALL: return "Ctrl+E";
            case COLLAPSE_ALL: return "Ctrl+Shift+E";
            case PARSE_JSON: return "Ctrl+Enter";
            case CLEAR_CONTENT: return "Ctrl+Delete";
            case SCHEMA_VALIDATION: return "Ctrl+Shift+V";
            default: return "未知";
        }
    }
    
    /**
     * 获取所有快捷键描述
     * 
     * @return 快捷键描述映射
     */
    public Map<String, String> getAllShortcutDescriptions() {
        Map<String, String> descriptions = new HashMap<>();
        descriptions.put("打开文件", getShortcutDescription(OPEN_FILE));
        descriptions.put("保存文件", getShortcutDescription(SAVE_FILE));
        descriptions.put("撤销", getShortcutDescription(UNDO));
        descriptions.put("重做", getShortcutDescription(REDO));
        descriptions.put("格式化JSON", getShortcutDescription(FORMAT_JSON));
        descriptions.put("压缩JSON", getShortcutDescription(COMPRESS_JSON));
        descriptions.put("搜索", getShortcutDescription(SEARCH));
        descriptions.put("展开所有", getShortcutDescription(EXPAND_ALL));
        descriptions.put("折叠所有", getShortcutDescription(COLLAPSE_ALL));
        descriptions.put("解析JSON", getShortcutDescription(PARSE_JSON));
        descriptions.put("清空内容", getShortcutDescription(CLEAR_CONTENT));
        descriptions.put("Schema验证", getShortcutDescription(SCHEMA_VALIDATION));
        return descriptions;
    }
    
    // ==================== 内部接口 ====================
    
    /**
     * 动作监听器接口
     */
    public interface ActionListener {
        void onOpenFile();
        void onSaveFile();
        void onUndo();
        void onRedo();
        void onFormatJson();
        void onCompressJson();
        void onSearch();
        void onExpandAll();
        void onCollapseAll();
        void onParseJson();
        void onClearContent();
        void onSchemaValidation();
    }
}
